/** ********************************************************************
 * File:           CKI.java 
 * Description:    UI for the Common Keyboard Interface
 * Authors:        Akshay,Abhinava,Revati,Arun 
 * Created:        Mon Oct 20 19:31:25 GMT 2008
 *
 * (C) Copyright 2008, MILE Lab, IISc
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 ** http://www.apache.org/licenses/LICENSE-2.0
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 *
 **********************************************************************/

/*
 * Common Keboard interface module
 *
 * SWT is used for the shell extension.
 * Main part is the interface.
 * 
 * @version 0.1
 */
/**
 * package core is the source package
 * for Common Keyboard Interface
 */
package core;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class CKI {

    public static void main(String[] args) throws FileNotFoundException {

        /*
         * Creating an instance of the SplashScreenMain class for the Splash
         * screen.The constructor takes in the number of milliseconds splash
         * screen will be active.
         */
        /*
         * final SplashScreenMain splash = new SplashScreenMain(3000);
         * splash.SplashScreenShow();
         *
         * The method has been modified to deal with the milliseconds in the
         * file SplashScreen.java itself
         */

        SplashScreen s = new SplashScreen();
        s.screen();

        // Creating the main display.Since SWT 3.1, support for multiple
        // displays has been
        // discontinued. SO at a time, only one active display can exist.
        final Display display = new Display();
        final Shell shell = new Shell(display);

        // the image path. The image File must be in the same directory/package
        // as that of the source file

        final Image image = new Image(display, CKI.class.getResourceAsStream("pifmgr1.ico"));

        // Creating the tray icon
        final Tray tray = display.getSystemTray();

        if (tray == null) {
            System.err.println("The system tray is not available");
        } else {
            final TrayItem item = new TrayItem(tray, SWT.NONE);
            item.setToolTipText("Common Keyboard Interface\nDouble click to visit CKI homepage");
            /* DefaultSelection is Double-click */
            item.addListener(SWT.DefaultSelection, new Listener() {

                public void handleEvent(Event event) {

                    Shell sh = new Shell(display, SWT.APPLICATION_MODAL);
                    sh.setImage(image);
                    MessageBox messageBox = new MessageBox(sh, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
                    messageBox.setText("Open Browser");

                    messageBox.setMessage("Dou you want to open CKI's webpage ??\n\n(Opens in the default internet browser)");
                    int rCode = messageBox.open();
                    if (rCode == SWT.YES) {
                        /*
                         * Opens the default browser. Or any program which opens
                         * .html files by default
                         */
                        Program pBrowse = Program.findProgram(".html");

                        pBrowse.execute("http://code.google.com/p/cki");

                    } else {

                        sh.dispose();
                    }

                }
            });

            final Menu menu = new Menu(shell, SWT.POP_UP); /*
             * The main menu
             * obtained when the tray is
             * right clicked
             */

            final MenuItem kannada = new MenuItem(menu, SWT.CASCADE);
            kannada.setText("Kannada");
            final Menu submenu = new Menu(shell, SWT.DROP_DOWN);
            kannada.setMenu(submenu);
            final MenuItem kgp = new MenuItem(submenu, SWT.PUSH);
            kgp.setText("KaGaPa");
            final MenuItem inscript = new MenuItem(submenu, SWT.PUSH);
            inscript.setText("Microsoft Inscript");

            final MenuItem tamil = new MenuItem(menu, SWT.CASCADE);
            tamil.setText("Tamil");
            final Menu submenu1 = new Menu(shell, SWT.DROP_DOWN);
            tamil.setMenu(submenu1);
            final MenuItem tamil99 = new MenuItem(submenu1, SWT.PUSH);
            tamil99.setText("Tamilnet99");

            new MenuItem(menu, SWT.SEPARATOR);

            final MenuItem help = new MenuItem(menu, SWT.PUSH);
            help.setText("Help");

            new MenuItem(menu, SWT.SEPARATOR);

            final MenuItem links = new MenuItem(menu, SWT.CASCADE);
            links.setText("Useful Links");
            final Menu submenu2 = new Menu(shell, SWT.DROP_DOWN);
            links.setMenu(submenu2);
            final MenuItem langtech = new MenuItem(submenu2, SWT.PUSH);
            langtech.setText("Language Technology");
            final MenuItem codegoogle = new MenuItem(submenu2, SWT.PUSH);
            codegoogle.setText("CKI code.google.com");
            final MenuItem ckidev = new MenuItem(submenu2, SWT.PUSH);
            ckidev.setText("CKI Developers Google group");
            final MenuItem ckiuser = new MenuItem(submenu2, SWT.PUSH);
            ckiuser.setText("CKI Users Google group");

            new MenuItem(menu, SWT.SEPARATOR);

            final MenuItem exit = new MenuItem(menu, SWT.PUSH);
            exit.setText("Exit");

            exit.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    // String q=s.substring(28, 35);
                    String d = exit.getText();
                    exit.setSelection(false);
                    if (d.compareTo("Exit") == 0) {

                        Shell sh = new Shell(display, SWT.APPLICATION_MODAL);
                        sh.setImage(image);
                        MessageBox messageBox = new MessageBox(sh, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
                        messageBox.setText("Exit...");

                        messageBox.setMessage("Are you sure to quit CKI??");
                        int rCode = messageBox.open();
                        if (rCode == SWT.YES) {
                            image.dispose();
                            shell.dispose();
                            display.dispose();
                            System.exit(0);

                        } else {

                            sh.dispose();
                        }
                    }
                }
            });

            help.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    // String q=s.substring(28, 35);
                    String d = help.getText();
                    exit.setSelection(false);
                    if (d.compareTo("Help") == 0) {

                        Shell sh = new Shell(display, SWT.APPLICATION_MODAL);
                        sh.setImage(image);
                        MessageBox messageBox = new MessageBox(sh, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
                        messageBox.setText("Opening CKI's help...");

                        messageBox.setMessage("Dou you want to open CKI's help ??\n\n(Requires an Internet connection)");
                        int rCode = messageBox.open();
                        if (rCode == SWT.YES) {
                            /*
                             * Opens the default browser. Or any program which
                             * opens .html files by default
                             */
                            Program pBrowse = Program.findProgram(".html");

                            pBrowse.execute("http://groups.google.com/group/cki-users/web/help-for-users");

                        } else {

                            sh.dispose();
                        }
                    }
                }
            });

            // All the kannada layouts go here
            {

                kgp.addListener(SWT.Selection, new Listener() {

                    public void handleEvent(Event event) {
                        String s = event.toString();
                        // String q=s.substring(28, 35);

                        kgp.setSelection(false);
                        {
                            String qk = s.substring(25, 31);

                            Shell sh = new Shell(display);
                            sh.setImage(image);
                            MessageBox messageBox = new MessageBox(sh, SWT.OK | SWT.ICON_INFORMATION);
                            messageBox.setText("Language Selection");
                            messageBox.setMessage("You have selected " + qk);
                            messageBox.open();
                            sh.dispose();
                            String path; /*
                             * this holds the path where the output
                             * file will be stored
                             */
                            try {
                                Shell shell = new Shell(display);
                                shell.setFocus();
                                shell.setImage(image);
                                FileDialog dialog = new FileDialog(shell,
                                        SWT.SAVE);

                                dialog.setFilterNames(new String[]{
                                            "Text Files", "CKI File",
                                            "All Files (*.*)"});
                                dialog.setFilterExtensions(new String[]{
                                            "*.txt", "*.cki", "*.*"}); // Wild
                                // cards
                                dialog.setFileName("ucodekannadaKGP.txt");
                                dialog.setText("Location to save the file");
                                path = dialog.open();
                                if (path == null) {
                                    /*
                                     * Shell sh1 = new Shell(display);
                                     * MessageBox messageBox1 = new MessageBox(
                                     * sh1, SWT.OK | SWT.ICON_ERROR);
                                     * messageBox1.setText("ERROR");
                                     *
                                     * messageBox1.setMessage(
                                     * "Select a path.\n\nIf you don't want to save, \npress OK in the file dialog.\nThe file will not be saved"
                                     * ); messageBox1.open(); sh.dispose();
                                     */
                                    throw new NullPointerException();

                                /*
                                 * the above exception is purposely thrown
                                 * to handle the situation when the Save
                                 * file dialog is closed by pressing
                                 * 'Cancel'. The editor opens only when a
                                 * valid path is chosen.
                                 */
                                }

                                String inp = null;
                                char s1 = 0;

                                try {

                                    InputDialog dialog1 = new InputDialog(
                                            display, "KaGaPa");
                                    inp = dialog1.open();

                                    try {
                                        s1 = inp.charAt(0);

                                    } catch (NullPointerException e1) {

                                        // Do nothing : If Cancel button is
                                        // pressed in the editor.
                                    } catch (StringIndexOutOfBoundsException e1) {

                                        Shell sh2 = new Shell(display);
                                        sh2.setImage(image);
                                        MessageBox mBox2 = new MessageBox(sh2,
                                                SWT.OK | SWT.ICON_ERROR);
                                        mBox2.setText("No Input Entered");

                                        mBox2.setMessage("You have not entered anything.\nGo back to the tray area.");
                                        mBox2.open();
                                        sh2.dispose();

                                    }

                                    if (s1 == 0) {
                                        // Do nothing
                                    } else {

                                        FileOutputStream out = new FileOutputStream(
                                                path);
                                        PrintStream p; // declare a print stream
                                        // object
                                        p = new PrintStream(out);
                                        // Connect print stream to the output
                                        // stream

                                        /*
                                         * now the convert method will simply
                                         * convert and the o/p is put to file
                                         * only after the user presses ok button
                                         */

                                        Kannada k = new Kannada();
                                        for (int i = 0; i < inp.length(); i++) {
                                            p.print(k.convert(inp.charAt(i)));
                                        }
                                        p.close();

                                    } /*NOT REQUIRED
                                 * else { Shell sh2 = new Shell(display);
                                 * sh2.setImage(image); MessageBox mBox1 =
                                 * new MessageBox(sh2, SWT.OK |
                                 * SWT.ICON_INFORMATION);
                                 * mBox1.setText("Language Selection");
                                 *
                                 * mBox1.setMessage(
                                 * "We have currently implemented only for '\u0C85'."
                                 * ); mBox1.open();
                                 *
                                 * sh2.dispose();
                                 *
                                 * }
                                 */

                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                } // declare a file output object
                            } catch (NullPointerException e) {
                                // Cancel button of the file save dialog
                                // do nothing
                            }

                        }
                    }
                });

                inscript.addListener(SWT.Selection, new Listener() {

                    public void handleEvent(Event event) {
                        String s = event.toString();
                        // String q=s.substring(28, 35);

                        inscript.setSelection(false);
                        {
                            String qk = s.substring(25, 43);

                            Shell sh = new Shell(display);
                            sh.setImage(image);
                            MessageBox messageBox = new MessageBox(sh, SWT.OK | SWT.ICON_INFORMATION);
                            messageBox.setText("Language Selection");

                            messageBox.setMessage("You have selected " + qk);
                            messageBox.open();
                            sh.dispose();
                            String path; /*
                             * this holds the path where the output
                             * file will be stored
                             */
                            try {
                                Shell shell = new Shell(display);
                                shell.setImage(image);
                                shell.setFocus();
                                FileDialog dialog = new FileDialog(shell,
                                        SWT.SAVE);

                                dialog.setFilterNames(new String[]{
                                            "Text Files", "CKI File",
                                            "All Files (*.*)"});
                                dialog.setFilterExtensions(new String[]{
                                            "*.txt", "*.cki", "*.*"}); // Wild
                                // cards
                                dialog.setFileName("ucodekannadaInscript.txt");
                                dialog.setText("Location to save the file");
                                path = dialog.open();
                                if (path == null) {
                                    /*
                                     * Shell sh1 = new Shell(display);
                                     * MessageBox messageBox1 = new MessageBox(
                                     * sh1, SWT.OK | SWT.ICON_ERROR);
                                     * messageBox1.setText("ERROR");
                                     *
                                     * messageBox1.setMessage(
                                     * "Select a path.\n\nIf you don't want to save, \npress OK in the file dialog.\nThe file will not be saved"
                                     * ); messageBox1.open(); sh.dispose();
                                     */
                                    throw new NullPointerException();

                                /*
                                 * the above exception is purposely thrown
                                 * to handle the situation when the Save
                                 * file dialog is closed by pressing
                                 * 'Cancel'. The editor opens only when a
                                 * valid path is chosen.
                                 */
                                }

                                String inp = null;
                                char s1 = 0;

                                try {

                                    InputDialog dialog1 = new InputDialog(
                                            display, "Microsoft Inscript");
                                    inp = dialog1.open();

                                    try {
                                        s1 = inp.charAt(0);
                                    } catch (NullPointerException e1) {

                                        // Do nothing : If Cancel button is
                                        // pressed in the editor.
                                    } catch (StringIndexOutOfBoundsException e1) {

                                        Shell sh2 = new Shell(display);
                                        sh2.setImage(image);
                                        MessageBox mBox2 = new MessageBox(sh2,
                                                SWT.OK | SWT.ICON_ERROR);
                                        mBox2.setText("No Input Entered");

                                        mBox2.setMessage("You have not entered anything.\nGo back to the tray area.");
                                        mBox2.open();
                                        sh2.dispose();

                                    }

                                    if (s1 == 0) {
                                        // Do nothing
                                    } else {

                                        FileOutputStream out = new FileOutputStream(
                                                path);
                                        PrintStream p; // declare a print stream
                                        // object
                                        p = new PrintStream(out); // Connect
                                        // print
                                        // stream to the
                                        // output stream

                                        Kannada k = new Kannada();
                                        for (int i = 0; i < inp.length(); i++) {
                                            p.print(k.convert(inp.charAt(i)));
                                        }
                                        p.close();
                                    }//end of else
									/*
                                 * NOT REQUIRED else { Shell sh2 = new
                                 * Shell(display); sh2.setImage(image);
                                 * MessageBox mBox1 = new MessageBox(sh2,
                                 * SWT.OK | SWT.ICON_INFORMATION);
                                 * mBox1.setText("Language Selection");
                                 *
                                 * mBox1.setMessage(
                                 * "We have currently implemented only for '\u0C85'."
                                 * ); mBox1.open();
                                 *
                                 * sh2.dispose();
                                 *
                                 * }
                                 */

                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                } // declare a file output object
                            } catch (NullPointerException e) {
                                // Cancel button of the file save dialog
                                // do nothing
                            }

                        }
                    }
                });

            }// End of kannada section

            // All the tamil layouts go here
            {
                tamil99.addListener(SWT.Selection, new Listener() {

                    public void handleEvent(Event event) {
                        String s = event.toString();
                        // String q=s.substring(28, 35);

                        tamil99.setSelection(false);
                        {
                            String qt = s.substring(25, 35);

                            Shell sh1 = new Shell(display);
                            sh1.setImage(image);
                            MessageBox mBox1 = new MessageBox(sh1, SWT.OK | SWT.ICON_INFORMATION);
                            mBox1.setText("Language Selection");

                            mBox1.setMessage("You have selected " + qt);
                            mBox1.open();

                            sh1.dispose();

                            String path; /*
                             * this holds the path where the output
                             * file will be stored
                             */
                            try {
                                Shell shell = new Shell(display);
                                shell.setImage(image);
                                FileDialog dialog = new FileDialog(shell,
                                        SWT.SAVE);
                                dialog.setFilterNames(new String[]{
                                            "Text Files", "CKI File",
                                            "All Files (*.*)"});
                                dialog.setFilterExtensions(new String[]{
                                            "*.txt", "*.cki", "*.*"}); // Wild
                                // cards
                                dialog.setFileName("ucodetamil99.txt");
                                dialog.setText("Location to save the file");
                                path = dialog.open();
                                if (path == null) {
                                    /*
                                     * Shell sh2 = new Shell(display);
                                     * MessageBox messageBox1 = new MessageBox(
                                     * sh2, SWT.OK | SWT.ICON_ERROR);
                                     * messageBox1.setText("ERROR");
                                     *
                                     * messageBox1.setMessage(
                                     * "Select a path.\n\nIf you don't want to save, "
                                     * +
                                     * "\npress OK in the file dialog.\nThe file will not be saved"
                                     * ); messageBox1.open(); sh2.dispose();
                                     */
                                    throw new NullPointerException();

                                /*
                                 * the above exception is purposely thrown
                                 * to handle the situation when the Save
                                 * file dialog is closed by pressing
                                 * 'Cancel'. The editor opens only when a
                                 * valid path is chosen.
                                 */
                                }

                                String inp = null;
                                char s1 = 0;
                                try {

                                    InputDialog dialog5 = new InputDialog(
                                            display, "Tamilnet99");
                                    inp = dialog5.open();

                                    try {
                                        s1 = inp.charAt(0);
                                    } catch (NullPointerException e) {

                                        // Do nothing : If cancel button is
                                        // pressed
                                    } catch (StringIndexOutOfBoundsException e1) {

                                        Shell sh2 = new Shell(display);
                                        sh2.setImage(image);
                                        MessageBox mBox2 = new MessageBox(sh2,
                                                SWT.OK | SWT.ICON_ERROR);
                                        mBox2.setText("No Input Entered");

                                        mBox2.setMessage("You have not entered anything.\nGo back to the tray area.");
                                        mBox2.open();
                                        sh2.dispose();

                                    }

                                    if (s1 == 0) {
                                        // Do nothing
                                    } else {

                                        FileOutputStream out = new FileOutputStream(
                                                path);
                                        PrintStream p; // declare a print stream
                                        // object
                                        p = new PrintStream(out); // Connect
                                        // print
                                        // stream to the
                                        // output stream
                                        Tamil t = new Tamil();
                                        for (int i = 0; i < inp.length(); i++) {
                                            p.print(t.convert(inp.charAt(i)));
                                        }
                                        p.close();
                                    }

                                /*NOT REQUIRED
                                 * else {
                                Shell sh2 = new Shell(display);
                                sh2.setImage(image);
                                MessageBox mBox2 = new MessageBox(sh2,
                                SWT.OK | SWT.ICON_INFORMATION);
                                mBox2.setText("Language Selection");

                                mBox2
                                .setMessage("We have currently implemented only for '\u0B85'");
                                mBox2.open();

                                sh2.dispose();
                                }*/
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                } // declare a file output object
                            } catch (NullPointerException e) {
                                // Cancel button of the file save dialog
                                // do nothing
                            }
                        }

                    }
                });

            }// End of tamil section

            langtech.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    Program pBrowse = Program.findProgram(".html");
                    pBrowse.execute("http://languagetechnology.org");

                }
            });

            codegoogle.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    Program pBrowse = Program.findProgram(".html");
                    pBrowse.execute("http://code.google.com/p/cki");

                }
            });

            ckidev.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    Program pBrowse = Program.findProgram(".html");
                    pBrowse.execute("http://groups.google.com/group/cki-dev/");

                }
            });

            ckiuser.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    Program pBrowse = Program.findProgram(".html");
                    pBrowse.execute("http://groups.google.com/group/cki-users");

                }
            });

            item.addListener(SWT.MenuDetect, new Listener() {

                public void handleEvent(Event event) {
                    menu.setVisible(true);
                }
            });
            item.setImage(image);
        }// end of else

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        image.dispose();
        shell.dispose();
        display.dispose();
    // label.dispose(); part of the previous segment of code.

    }// end of main
}// end of class CKI
